package com.yuelan.apartment.controller;

import com.yuelan.apartment.domain.Notice;
import com.yuelan.apartment.service.NoticeService;
import com.yuelan.common.core.web.controller.BaseController;
import com.yuelan.common.core.web.domain.AjaxResult;
import com.yuelan.common.core.web.page.PageDomain;
import com.yuelan.common.core.web.page.TableDataInfo;
import com.yuelan.common.log.annotation.Log;
import com.yuelan.common.log.enums.BusinessType;
import com.yuelan.common.security.annotation.Logical;
import com.yuelan.common.security.annotation.RequiresPermissions;
import com.yuelan.common.security.annotation.RequiresRoles;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @description: 通知与公告
 * @Author: ZhaoYi
 * @date: 24-6-29 19:55
 */
@RestController
@RequestMapping("notice")
public class NoticeController extends BaseController {

    @Autowired
    private NoticeService noticeService;


    @PostMapping
    @RequiresRoles(value = {"admin", "common"}, logical = Logical.OR)
    @Log(title = "发布公告", businessType = BusinessType.INSERT)
    public AjaxResult publish(@RequestBody Notice notice) {
        return toAjax(noticeService.publishNotice(notice));
    }


    @GetMapping("/{id}")
    @RequiresRoles(value = {"admin", "common"}, logical = Logical.OR)
    @Log(title = "查看公告", businessType = BusinessType.QUERY)
    public AjaxResult view(@PathVariable Long id) {
        return success(noticeService.view(id));
    }


    @GetMapping("/list")
    @RequiresRoles(value = {"admin", "common"}, logical = Logical.OR)
    @Log(title = "查看公告列表", businessType = BusinessType.QUERY)
    public TableDataInfo noticeList(int pageNum, int pageSize) {
        return noticeService.noticeList(pageNum, pageSize);
    }


    @DeleteMapping("/{id}")
    @RequiresRoles(value = {"admin", "common"}, logical = Logical.OR)
    @Log(title = "删除公告", businessType = BusinessType.DELETE)
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(noticeService.removeNotice(id));
    }


    @PutMapping
    @RequiresRoles(value = {"admin", "common"}, logical = Logical.OR)
    @Log(title = "编辑公告", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@RequestBody Notice notice) {
        return toAjax(noticeService.editNotice(notice));
    }
}
